package com.spring.aop.exposeproxy;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

import java.lang.reflect.Method;

/**
 * @ClassName MethodInvokeInterceptor
 * @Description
 * @Author suncx
 * @Date 2020/6/27 13:40
 * @Version 1.0
 */
public class MethodInvokeInterceptor implements MethodInterceptor {

    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.println("MethodInvokeInterceptor start... "+invocation.getMethod()+" is called on "+
                invocation.getThis()+" with args "+invocation.getArguments());
        Method method = invocation.getMethod();
        ScxMethodInterceptor annotation = getAnnotation(method);
        System.out.println("MethodInvokeInterceptor end...");
        if (annotation == null) {
            return invocation.proceed();
        }
        return null;
    }

    private ScxMethodInterceptor getAnnotation(Method method) {
        // 如果有多个annotation 似乎就不好用了 如放在controller上 由于已经有了@RequestMapping注解了 所以...
        if (method.isAnnotationPresent(ScxMethodInterceptor.class)) {
            return method.getAnnotation(ScxMethodInterceptor.class);
        }
        return null;
    }
}
